<?php
/**
 * User:于小涛
 * Date:2020/9/15
 * Time:14:07
 */
namespace app\api\controller\promotion;

use app\api\BaseController;
use app\api\model\promotion\PromotionRecord as PromotionRecordModel;
use app\api\model\promotion\SignRecord;
use app\api\model\promotion\VisitRecord;
use app\api\model\system\Channel;
use app\api\validate\promotion\PromotionRecord as PromotionRecordValidate;
use think\App;

class Statistic extends BaseController{
    private $obj_promotion_record_model=null;
    private $obj_visit_record_model=null;
    private $obj_sign_record_model=null;
    private $obj_promotion_record_validate=null;
    private $obj_channel_model=null;
    private $arr_bind_channel_ids=[];
    private $arr_admin_list=[];
    private $arr_channel_list=[];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_promotion_record_model = new PromotionRecordModel();
        $this->obj_visit_record_model = new VisitRecord();
        $this->obj_sign_record_model = new SignRecord();
        $this->obj_promotion_record_validate = new PromotionRecordValidate();
        $this->obj_channel_model = new Channel();
        //获取推广专员绑定的渠道ids
        $arr_bind_channel_ids=$this->obj_channel_model
            ->alias("c")
            ->field('c.id')
            ->leftjoin('channel_bind_admin cba','cba.channel_id=c.id')
            ->where('cba.admin_id',$this->request->arr_admin_info['id'])
            ->select()
            ->toArray();
        foreach($arr_bind_channel_ids as $arr_item){
            $this->arr_bind_channel_ids[]=$arr_item['id'];
        }
        $this->arr_admin_list=$this->getAllAdmin();
        $this->arr_channel_list=$this->obj_channel_model->select()->toArray();
        $this->arr_channel_list=array_item_key_to_self_key($this->arr_channel_list,'id');
    }

    public function getPromotionStatisticData(){
        $str_begin_date=$this->request->param('begin_date',date("Y-m-d",strtotime("-7day")));
        $str_end_date=$this->request->param('begin_date',date("Y-m-d"));
        $arr_where=[
            ['date','>=',$str_begin_date],
            ['date','<=',$str_end_date],
        ];
        if(!empty($this->request->param('channel_ids'))){
            $arr_where[]=['channel_id','in',$this->request->param('channel_ids')];
        }
        if(!empty($this->request->param('tuiguang_admin_ids'))){
            $arr_where[]=['tuiguang_admin_id','in',$this->request->param('tuiguang_admin_ids')];
        }
        $arr_data=$this->obj_promotion_record_model
            ->field('channel_id,tuiguang_admin_id,sum(all_num) as total_all_num,sum(valid_num) as total_valid_num')
            ->where($arr_where)
            ->group("tuiguang_admin_id")
            ->group("channel_id")
            ->select()
            ->toArray();
        $arr_re_data=[
            [['content'=>'姓名','css'=>''],['content'=>'渠道','css'=>''],['content'=>'总推送量','css'=>''],['content'=>'有效量','css'=>''],['content'=>'有效率','css'=>''],['content'=>'签单量','css'=>''],['content'=>'到访量','css'=>'']]
        ];
        foreach($arr_data as $arr_item){
            $arr_re_data_item=[
                ['content'=>$this->arr_admin_list[$arr_item['tuiguang_admin_id']]['name'],'css'=>''],
                ['content'=>$this->arr_channel_list[$arr_item['channel_id']]['name'],'css'=>''],
                ['content'=>$arr_item['total_all_num'],'css'=>''],
                ['content'=>$arr_item['total_valid_num'],'css'=>''],
            ];
            //计算有效率
            if(empty($arr_item['total_all_num'])){
                $str_youxiaolv=0;
            }else{
                if(empty($arr_item['total_valid_num'])){
                    $str_youxiaolv='0.0%';
                }else{
                    $str_youxiaolv=(number_format($arr_item['total_valid_num']/$arr_item['total_all_num'],3,'.','')*100).'%';
                }
            }
            $arr_re_data_item[]=['content'=>$str_youxiaolv,'css'=>''];
            //查询签单量
            $arr_where_sign=[
                ['date','>=',$str_begin_date],
                ['date','<=',$str_end_date],
                ['channel_id','=',$arr_item['channel_id']],
                ['tuiguang_admin_id','=',$arr_item['tuiguang_admin_id']]
            ];
            $arr_sign_data=$this->obj_sign_record_model
                ->field("sum(num) as total_num")
                ->where($arr_where_sign)
                ->select()
                ->toArray();
            $arr_re_data_item[]=['content'=>empty($arr_sign_data[0]['total_num'])?0:$arr_sign_data[0]['total_num'],'css'=>''];
            //查询到访量
            $arr_where_visit=[
                ['date','>=',$str_begin_date],
                ['date','<=',$str_end_date],
                ['channel_id','=',$arr_item['channel_id']],
                ['tuiguang_admin_id','=',$arr_item['tuiguang_admin_id']]
            ];
            $arr_visit_data=$this->obj_visit_record_model
                ->field("sum(num) as total_num")
                ->where($arr_where_visit)
                ->select()
                ->toArray();
            $arr_re_data_item[]=['content'=>empty($arr_visit_data[0]['total_num'])?0:$arr_visit_data[0]['total_num'],'css'=>''];
            $arr_re_data[]=$arr_re_data_item;
        }
        return output_data($arr_re_data);
    }

    public function getValidStatisticData(){
        $str_begin_date=$this->request->param('begin_date',date("Y-m-d",strtotime("-7day")));
        $str_end_date=$this->request->param('begin_date',date("Y-m-d"));
        $arr_where=[
            ['date','>=',$str_begin_date],
            ['date','<=',$str_end_date],
        ];
        if(!empty($this->request->param('channel_ids'))){
            $arr_where[]=['channel_id','in',$this->request->param('channel_ids')];
        }
        if(!empty($this->request->param('chuangye_admin_ids'))){
            $arr_where[]=['tuiguang_admin_id','in',$this->request->param('tuiguang_admin_ids')];
        }
        $arr_data=$this->obj_promotion_record_model
            ->field('channel_id,chuangye_admin_id,sum(all_num) as total_all_num,sum(valid_num) as total_valid_num')
            ->where($arr_where)
            ->group("chuangye_admin_id")
            ->group("channel_id")
            ->select()
            ->toArray();
        $arr_re_data=[
            [['content'=>'姓名','css'=>''],['content'=>'有效资源','css'=>''],['content'=>'到访量','css'=>''],['content'=>'签约量','css'=>''],['content'=>'到访率','css'=>''],['content'=>'签约率','css'=>'']]
        ];
        foreach($arr_data as $arr_item){
            $int_valid_num=empty($arr_item['total_valid_num'])?0:$arr_item['total_valid_num'];
            $arr_re_data_item=[
                ['content'=>$this->arr_admin_list[$arr_item['chuangye_admin_id']]['name'],'css'=>''],
                ['content'=>$int_valid_num,'css'=>''],
            ];
            //查询到访量
            $arr_where_visit=[
                ['date','>=',$str_begin_date],
                ['date','<=',$str_end_date],
                ['channel_id','=',$arr_item['channel_id']],
                ['chuangye_admin_id','=',$arr_item['chuangye_admin_id']]
            ];
            $arr_visit_data=$this->obj_visit_record_model
                ->field("sum(num) as total_num")
                ->where($arr_where_visit)
                ->select()
                ->toArray();
            $int_visit_num=empty($arr_visit_data[0]['total_num'])?0:$arr_visit_data[0]['total_num'];
            $arr_re_data_item[]=['content'=>$int_visit_num,'css'=>''];
            //查询签单量
            $arr_where_sign=[
                ['date','>=',$str_begin_date],
                ['date','<=',$str_end_date],
                ['channel_id','=',$arr_item['channel_id']],
                ['chuangye_admin_id','=',$arr_item['chuangye_admin_id']]
            ];
            $arr_sign_data=$this->obj_sign_record_model
                ->field("sum(num) as total_num")
                ->where($arr_where_sign)
                ->select()
                ->toArray();
            $int_sign_num=empty($arr_sign_data[0]['total_num'])?0:$arr_sign_data[0]['total_num'];
            $arr_re_data_item[]=['content'=>$int_sign_num,'css'=>''];
            //计算到访率
            if(empty($int_valid_num)){
                $str_daofanglv=0;
            }else{
                if(empty($int_visit_num)){
                    $str_daofanglv='0.0%';
                }else{
                    $str_daofanglv=(number_format($int_visit_num/$int_valid_num,3,'.','')*100).'%';
                }
            }
            $arr_re_data_item[]=['content'=>$str_daofanglv,'css'=>''];
            //计算签约率
            if(empty($int_valid_num)){
                $str_qianyuelv=0;
            }else{
                if(empty($int_sign_num)){
                    $str_qianyuelv='0.0%';
                }else{
                    $str_qianyuelv=(number_format($int_sign_num/$int_valid_num,3,'.','')*100).'%';
                }
            }
            $arr_re_data_item[]=['content'=>$str_qianyuelv,'css'=>''];
            $arr_re_data[]=$arr_re_data_item;
        }
        return output_data($arr_re_data);
    }

}